package com.sec.samsung.android.app.baymax.view.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.controll.BaymaxRequestManager;
import com.sec.samsung.android.app.baymax.controll.IRequestListener;
import com.sec.samsung.android.app.baymax.log.AIOLog;
import com.sec.samsung.android.app.baymax.model.reponse.ResponseDiseaseData;
import com.sec.samsung.android.app.baymax.utils.StringUtility;

public class ResultActivity extends Activity {
    public final static String STRING_END_LINE = "\n";
    private TextView mTxtDisease;
    private TextView mBtnRiskLevel;
    private TextView mTxtDescription;
    private TextView mTxtTreatment;
    private TextView mBtnClickToSeeMore;
    private ProgressBar mProgressBar;
    private RelativeLayout mPageResult;
    private LinearLayout mLayoutNoConnection;

    private ResponseDiseaseData mResponseDiseaseData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        initLayout();

        initData();
    }

    private void initData() {
        String answerData = getIntent().getExtras().getString("answer_data");
        BaymaxRequestManager.getInstance(this).postListAnswer(answerData, new IRequestListener() {

            @Override
            public void onResponse(String response) {
                mLayoutNoConnection.setVisibility(View.GONE);
                mPageResult.setVisibility(View.VISIBLE);

                Gson gson = new GsonBuilder().serializeNulls().create();
                if (StringUtility.isEmpty(response))
                    finish();
                mResponseDiseaseData = gson.fromJson(response, ResponseDiseaseData.class);
                if (mResponseDiseaseData == null)
                    return;
                updateLayout(mResponseDiseaseData);
            }

            @Override
            public void onErrorResponse(String errorMessage) {
                mLayoutNoConnection.setVisibility(View.VISIBLE);
                mPageResult.setVisibility(View.GONE);
            }
        }, this);
    }

    private void updateLayout(ResponseDiseaseData responseData) {
        AIOLog.d("result:percecnt:" + responseData.value.getPercent());
        int percent = responseData.value.getPercent();
        mTxtDisease.setText(responseData.value.getDisease().getName());
        mBtnRiskLevel.setText(getString(R.string.high_risk, String.valueOf(percent) + "%"));
        mTxtDescription.setText(responseData.value.getDisease().getDescription());
        mTxtTreatment.setText(responseData.value.getDisease().getTreatment());

        mProgressBar.setProgress(50);
    }

    private void initLayout() {
        mTxtDisease = (TextView) findViewById(R.id.txt_disease);
        mBtnRiskLevel = (TextView) findViewById(R.id.txt_disease_level);
        mBtnClickToSeeMore = (TextView) findViewById(R.id.txt_click_to_see);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_disease_level);
        mProgressBar.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);
        mPageResult = (RelativeLayout) findViewById(R.id.page_result);
        mLayoutNoConnection = (LinearLayout) findViewById(R.id.layout_no_connection);
        mTxtDescription = (TextView) findViewById(R.id.txt_description);
        mTxtTreatment = (TextView) findViewById(R.id.txt_treatment);

        mProgressBar.setMax(100);

        mBtnClickToSeeMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mResponseDiseaseData.value.getDisease().getUri()));
                    startActivity(myIntent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(ResultActivity.this, "No application can handle this request."
                        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }

    public void OnClickBackBtn(View v) {
        finish();
    }

    public void onClickShareBtn(View v) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, getSharedText());
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));
    }

    public void onClickReportBtn(View v) {
        Intent email = new Intent(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
        email.putExtra(Intent.EXTRA_SUBJECT, "[Baymax]Disease report");
        email.putExtra(Intent.EXTRA_TEXT, getSharedText()); 
        email.setType("message/rfc822");
        startActivity(Intent.createChooser(email, "Select Email Client"));
    }

    public void onClickTryAgain(View v) {
        initData();
    }

    public void onClickToSeeMore(View v) {

    }

    private String getSharedText() {
        Resources res = getResources();
        StringBuilder builder = new StringBuilder();
        builder.append(res.getString(R.string.you_got_disease, mResponseDiseaseData.value.getDisease().getName()))
                .append(STRING_END_LINE);
        builder.append(res.getString(R.string.description)).append(STRING_END_LINE);
        builder.append(mResponseDiseaseData.value.getDisease().getDescription()).append(STRING_END_LINE);
        builder.append(res.getString(R.string.treatment)).append(STRING_END_LINE);
        builder.append(mResponseDiseaseData.value.getDisease().getTreatment()).append(STRING_END_LINE);
        return builder.toString();
    }

    public void onClickStartOver(View v){
        Intent intent = new Intent(this, ProfileActivity.class);
        startActivity(intent);
        finish();
    }
}
